package emulator.shell.GUI.symbols;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;

import emulator.GUI.PersistentFrame;
import emulator.shell.GUI.ToolBarWindow;

@SuppressWarnings("serial")
public class SymbolTableView extends PersistentFrame
{
	private SymbolTablePresenter presenter;
	private SymbolTableVM viewModel;
	private JTable symbolTable;
	private JToolBar toolbar;

	public SymbolTableView(String appName, SymbolTablePresenter presenter)
	{
		super("Symbols", appName);
		this.presenter = presenter;
	}

	@Override
	public void windowClosing(WindowEvent arg0)
	{
		super.windowClosing(arg0);
		presenter.onCloseView();
	}
	
	public void initView(SymbolTableVM viewModel)
	{
		getContentPane().setLayout(new BorderLayout());
		
		this.viewModel = viewModel;
		
		symbolTable = new JTable();
		symbolTable.setAutoCreateRowSorter(true);
		symbolTable.setModel(viewModel.getSymbolTableModel());
		symbolTable.addMouseListener(new SymbolTableListener());
		
		toolbar = new JToolBar();
		toolbar.add(ToolBarWindow.makeNavigationButton("images/file_load.png", "Load symbols from file", "Load", new LoadSymbols()));
		toolbar.add(ToolBarWindow.makeNavigationButton("images/file_save.png", "Save symbols to file", "Save", new SaveSymbols()));
		toolbar.addSeparator();
		toolbar.add(ToolBarWindow.makeNavigationButton("images/delete_icon.png", "Delete symbols", "Delete", new DeleteSymbols()));

		add(toolbar, BorderLayout.PAGE_START);
		getContentPane().add(new JScrollPane(symbolTable), BorderLayout.CENTER);
	}

	public void updateViewModel(SymbolTableVM viewModel)
	{
		this.viewModel = viewModel;
		symbolTable.setModel(viewModel.getSymbolTableModel());
	}
	
	public class SymbolTableListener extends MouseAdapter
	{
		@Override
		public void mousePressed(MouseEvent e)
		{
			if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2)
			{
				int rowIndex = symbolTable.rowAtPoint(e.getPoint());
				int modelRow = symbolTable.convertRowIndexToModel(rowIndex);
				presenter.showCode(viewModel.getAddressAtSymbolLine(modelRow));
			}
		}
	}

	public class LoadSymbols implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			FileDialog dialog = new FileDialog(SymbolTableView.this, "Load Symbols from File", FileDialog.LOAD);
			dialog.setVisible(true);
			if (dialog.getDirectory() != null && dialog.getFile() != null)
			{
				String selected_file = dialog.getDirectory() + dialog.getFile();
				if (!selected_file.isEmpty())
				{
					presenter.loadSymbols(selected_file);
				}
			}
		}
	}

	public class SaveSymbols implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			SymbolSourceTableModel tableModel = new SymbolSourceTableModel(viewModel.getSources(true));
			
			SaveSymbolsDialog dialog = new SaveSymbolsDialog(SymbolTableView.this, tableModel);
			dialog.setVisible(true);
			
			if (dialog.getResult())
			{
				presenter.saveSymbols(dialog.getFileName(), tableModel.getSelectedSources());
			}
		}
	}

	public class DeleteSymbols implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			SymbolSourceTableModel tableModel = new SymbolSourceTableModel(viewModel.getSources(true));
			
			DeleteSymbolsDialog dialog = new DeleteSymbolsDialog(SymbolTableView.this, tableModel);
			dialog.setVisible(true);
			
			if (dialog.getResult())
			{
				presenter.deleteSymbols(tableModel.getSelectedSources());
			}
		}
	}
}
